package com.open.rbac.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;

/**
 * Swagger文档配置
 * <a href="http://127.0.0.1:11000/swagger-ui/index.html">
 * Swagger地址
 * </a>
 *
 * @author Riche's
 * @since 2025/5/24
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public List<GroupedOpenApi> groups() {
        return List.of(
                GroupedOpenApi.builder().group("用户管理").pathsToMatch("/v1/sys/user/**").build(),
                GroupedOpenApi.builder().group("角色管理").pathsToMatch("/v1/sys/role/**").build(),
                GroupedOpenApi.builder().group("组织机构管理").pathsToMatch("/v1/sys/organization/**").build(),
                GroupedOpenApi.builder().group("菜单管理").pathsToMatch("/v1/sys/menu/**").build()
        );
    }

    @Bean
    public OpenAPI openApi() {
        return new OpenAPI().info(new Info().title("Open Rbac")
                        .description("RBAC Restful API")
                        .version("1.0.0")
                        .termsOfService("https://www.rbac.com/d/about/introduce") // 服务条款
                        .license(new License().name("License").url("https://www.rbac.com")))
                .addSecurityItem(new SecurityRequirement().addList("auth"))
                .components(new Components().addSecuritySchemes("auth", new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .in(SecurityScheme.In.HEADER)
                        .name("Authorization")
                        .description("Example: 'Bearer {{token}}'")))
                .externalDocs(new ExternalDocumentation()
                        .description("Open Life Issues Documentation")
                        .url("https://www.rbac.com/d/about/join"));
    }

}
